<?php

namespace App\Http\Controllers\Project;

use App\Http\Requests\Project\ContractRequest;
use App\Models\Project\Contract;
use App\Models\Project\Progress;
use App\Services\Project\AttachmentService;
use App\Services\Project\ContractService;
use App\Services\Project\ProgressService;
use App\Services\Project\ProgressStatusService;
use App\Traits\Admin\SuccessOrFailure;
use Daijulong\LaravelRoles\Permission;
use Dotenv\Util\Str;
use Illuminate\Http\Request;


class ContractController extends Controller
{
    use SuccessOrFailure;


    private $permission = 'common.sign_company';

    protected $progressService;
    protected $contractService;

    public function __construct(ContractService $contractService, ProgressService $progressService)
    {
        $this->contractService = $contractService;
        $this->progressService = $progressService;
    }

    public function index($request, $progresses)
    {
        return Contract::query()->whereIn('progress_id', $progresses)->orderBy('id','desc')->paginate();

    }


    public function create()
    {
        $this->canOrFailure($this->permission);

        return view('project.contract.create');
    }

    public function store(ContractRequest $request)
    {
        $this->canOrFailure($this->permission);
        $messages = $this->contractService->validates($request);
        if ($messages) {
            return $this->failure(implode(',', $messages));
        }

        $res = $this->contractService->updateOrCreate($request);
        if (!$res) {
            return $this->failure('添加失败');
        }

        return $this->success('', route('project.contract.intentionContract'));
    }

    public function edit($id)
    {
        $this->canOrFailure($this->permission);

        $contract = $this->getContractOrFailure($id);
        $file_url = AttachmentService::getFileByType($contract->id, AttachmentService::TYPE_CONTRACT);

        return view('project.contract.edit', compact('contract', 'file_url'));
    }

    public function update(ContractRequest $request, $id)
    {
        $this->canOrFailure($this->permission);

        $contract = $this->getContractOrFailure($id);
        $messages = $this->contractService->validates($request);
        if ($messages) {
            return $this->failure(implode(',', $messages));
        }
        $res = $this->contractService->updateOrCreate($request, $contract);
        if (!$res) {
            return $this->failure('提交失败');
        }

        return $this->success('', route('project.contract.intentionContract'));
    }

    public function show($id)
    {
        $this->canOrFailure($this->permission);

        $contract = $this->getContractOrFailure($id);
        return view('project.contract.show', compact('contract'));
    }

    public function destroy($id)
    {
        $this->canOrFailure($this->permission);

        $sign_company = $this->getContractOrFailure($id);
        $sign_company->delete();
        return $this->success();
    }

    private function getContractOrFailure($id)
    {
        $contract = Contract::query()->find($id);
        if (!$contract) {
            response($this->failure('值不存在！'), 404)->send();
            exit;
        }
        return $contract;
    }

    public function intentionContract(Request $request)
    {
        $menu = [
            'title' => '意向合同',
            'id'    => 'intention_contract',
        ];
        $progresses = $this->progressService->getProgressByStage(Progress::STAGE_COMMUNICATE)->pluck('id')->toArray();
        $contracts = $this->index($request, $progresses);


        return view('project.contract.index', compact('contracts', 'menu', 'progresses'));
    }

    public function cancelled(Request $request)
    {
        $menu = [
            'title' => '作废合同',
            'id'    => 'cancelled',
        ];
        $progress_id = get_progress_id(ProgressStatusService::CANCELLED);
        $progresses = [$progress_id];
        $contracts = $this->index($request, $progresses);
        return view('project.contract.index', compact('contracts', 'menu'));

    }

    public function formal(Request $request)
    {
        $menu = [
            'title' => '正式合同',
            'id'    => 'formal',
        ];
        $progress_id = get_progress_id(ProgressStatusService::CANCELLED);
        $progresses = [$progress_id];
        $contracts = $this->index($request, $progresses);
        return view('project.contract.index', compact('contracts', 'menu'));
    }
}


